<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Licensed ( https://opensource.org/licenses/MIT )
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// +----------------------------------------------------------------------
// | 短信
// +----------------------------------------------------------------------
namespace app\sso\http\api\admin;

use Inphp\Core\Config;
use Inphp\Core\Db\Db;
use Inphp\Core\Modules;
use Inphp\Core\Object\Message;
use Inphp\Core\Util\Str;

#[\app\admin\attributes\auth(null, true)] class sms
{
    /**
     * 配置
     * @return array
     */
    public function config(): array
    {
        $config = Modules::getModule("sso")->getConfig("sms");
        return $config ?? [];
    }

    /**
     * 保存配置，配置保存后，会生成一个配置文件到 public/sso/config.php
     * @return Message
     */
    public function saveConfig(): Message
    {
        $debug = POST("debug");
        $debug = $debug == 1;
        //短信有效时间
        $lifeTime = POST("lifeTime");
        $lifeTime = is_numeric($lifeTime) && $lifeTime > 300 ? ceil($lifeTime) : 600;
        //
        $account = POST("account");
        $account = !empty($account) ? trim($account) : null;
        //
        $secret = POST("secret");
        $secret = !empty($secret) ? trim($secret) : null;
        //
        $tempId = POST("tempId");
        $tempId = is_numeric($tempId) && $tempId > 0 ? ceil($tempId) : 0;
        $tempId = floor($tempId);
        //
        $appId = POST("appId");
        $appId = !empty($appId) ? Str::trim($appId) : null;
        //
        $signName = POST("signName");
        $signName = !empty($signName) ? trim($signName) : null;
        //国际短信
        //
        $international_account = POST("international_account");
        $international_account = !empty($international_account) ? trim($international_account) : null;
        //
        $international_secret = POST("international_secret");
        $international_secret = !empty($international_secret) ? trim($international_secret) : null;
        //
        $international_tempId = POST("international_tempId");
        $international_tempId = is_numeric($international_tempId) && $international_tempId > 0 ? ceil($international_tempId) : 0;
        //
        $international_appId = POST("international_appId");
        $international_appId = !empty($international_appId) ? Str::trim($international_appId) : null;
        //
        $international_signName = POST("international_signName");
        $international_signName = !empty($international_signName) ? trim($international_signName) : null;

        //组合
        $sms = compact("debug", "lifeTime", "account", "secret", "tempId", "appId", "signName");
        $sms["international"] = [
            "account"       => $international_account,
            "secret"        => $international_secret,
            "appId"         => $international_appId,
            "tempId"        => floor($international_tempId),
            "signName"      => $international_signName
        ];
        //打包
        $config = Modules::getModule("sso")->getConfig(null, "config");
        $sms = array_merge($config["sms"], $sms);
        $config = array_merge($config, ["sms" => $sms]);
        if (Modules::getModule("sso")->saveConfig("config", $config)) {
            return httpMessage(0, "配置保存成功");
        }
        return httpMessage(9, "保存配置失败");
    }

    /**
     * 短信发送记录
     * @return array
     */
    public function list(): array
    {
        //查询
        $db = Db::from("sso_sms");

        //短信状态
        $state = GET("state");
        $state = $state == 1 || $state == 2 || $state == '0' ? $state : null;
        if (!is_null($state)) {
            $db->where("state", $state);
        }

        //发送状态
        $send = GET("send");
        $send = $send == 1 || $send == 2 || $send == '0' ? $send : null;
        if (!is_null($send)) {
            $db->where("send", $send);
        }

        //搜索
        $keyword = GET("keyword");
        $keyword = !empty($keyword) ? Str::trim($keyword) : null;
        if (!empty($keyword) && is_numeric($keyword) && strlen($keyword) < 12) {
            $db->whereLike("phone", "%{$keyword}%");
        }

        //行数
        $rows = $db->rows();
        //当前页数
        $page = GET('page', 1);
        $page = is_numeric($page) && $page > 0 ? ceil($page) : 1;
        //总页数
        $pages = 1;
        //列表数据
        $list = [];
        if ($rows > 0) {
            $total = GET("total", 30);
            $total = is_numeric($total) && $total > 2 ? $total : 30;
            $pages = ceil($rows / $total);
            $page = $page > $pages ? $pages : $page;
            $offset = $total * ($page - 1);
            $list = $db->orderBy("id", "desc")->get($total, $offset);
        }
        //返回列表数据，包含行数、页码、总页数、数据列表
        return compact("rows", "page", "pages", "list");
    }
}